const net = require("net");
const MyTransformCode = require("../02tcp-nianbao/myTransform.js");

const client = net.createConnection({
  host: "127.0.0.1",
  port: 1234,
});
let overageBuffer = null;
let ts = new MyTransformCode();

// client.write("我的一天");
// client.write("我的一天");
// client.write("我的一天");
// client.write("我的一天");
// client.write("我的一天");
client.write(ts.encode("我的一天-kdkdkk"));
client.write(ts.encode("我的一天-kdkdkk1"));
client.write(ts.encode("我的一天-kdkdkk2"));
client.write(ts.encode("我的一天-kdkdkk3"));
client.write(ts.encode("我的一天-kdkdkk4"));

client.on("data", (chunk) => {
  // console.log(chunk.toString());
  if (overageBuffer) {
    chunk = Buffer.concat([overageBuffer, chunk]);
  }
  let packageLen = 0;
  while ((packageLen = ts.getPackageLen(chunk))) {
    const packageCon = chunk.slice(0, packageLen);

    chunk = chunk.slice(packageLen);

    const ret = ts.decode(packageCon);
    console.log(ret);
    // client.write(ts.encode(ret.body, ret.serialNum));
  }
  overageBuffer = chunk;
});
